import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/api/calendar.dart';
import 'package:geomancy/models/daily_horsecope_model.dart';
import 'package:geomancy/providers/user_provider.dart';
import 'package:geomancy/styles/index.dart';
import 'package:geomancy/tools/constant.dart';
import 'package:geomancy/tools/utils.dart';
import 'package:geomancy/widgets/common_header.dart';
import 'package:geomancy/widgets/common_shadow_text.dart';
import 'package:geomancy/widgets/common_text.dart';
import 'package:geomancy/widgets/stroke_text.dart';
import 'package:lunar/calendar/Lunar.dart';
import 'package:provider/provider.dart';

class AnimalLuckPage extends StatefulWidget {
  ///接受一个生肖属相index
  final int animalIndex;
  const AnimalLuckPage({super.key, required this.animalIndex});

  @override
  State<AnimalLuckPage> createState() => _AnimalLuckPageState();
}

class _AnimalLuckPageState extends State<AnimalLuckPage> {
  late String animalText;
  late String animalPinYin;
  late Map<String, dynamic> ganZhiResult;
  late String date;
  late String weekend;
  //今日运势
  DailyHoroscopeResult? dailyHoroscopeResult;

  @override
  void initState() {
    super.initState();
    final now = DateTime.now();
    animalText = Utils.getZodiacTxtBybIndex(widget.animalIndex);
    animalPinYin = Utils.getZodiacPinYinBybIndex(widget.animalIndex);
    ganZhiResult = Utils.getLunarTime(now);
    date = Utils.getFormatDate(dateTime: now, format: 'YYYY年MM月DD日');
    weekend = Utils.getDayOfWeek(now);
    getDailyHoroscope();
  }

  ///获取今日运势
  Future getDailyHoroscope() async {
    final user = context.read<UserProvider>().getUserInfo;
    if (user?.birthday != null || user?.birthday != '') {
      final zodiacIndex = Utils.getZodiacIndex(user!.birthday!.split(' ')[0]);
      final result = await CalendarAPI.getDailyHoroscope(context, Utils.getZodiacPinYinBybIndex(zodiacIndex));
      setState(() {
        dailyHoroscopeResult = result;
      });
    }
  }

  ///运势item
  Widget rowItem({required String txt, required String url, int num = 0, required String content}) {
    return Container(
      margin: EdgeInsets.only(bottom: 40.w),
      child: Column(
        children: [
          Row(
            children: [
              StrokeText(txt: txt, fontSize: 30.sp, letterSpacing: 1.5),
              SizedBox(width: 30.w),
              Wrap(
                spacing: 10.w,
                children: List.filled(5, 0).asMap().keys.map((index) {
                  return SizedBox(
                    width: 27.w,
                    height: 22.w,
                    child: Image.asset(index < num ? '${url}_active.png' : '$url.png', height: 22.w),
                  );
                }).toList(),
              )
            ],
          ),
          SizedBox(height: 20.w),
          CommonText(txt: content, fontSize: 30.sp, height: 1.4)
        ],
      ),
    );
  }

  ///container样式
  BoxDecoration containerStyle() {
    return BoxDecoration(
        color: ColorStyle.themeLightColor,
        border: Border.all(width: 5.w, color: ColorStyle.borderColor),
        borderRadius: BorderRadius.all(Radius.circular(60.w))
    );
  }

  ///日期转化成农历
  String transformLunarChineseDay() {
    Lunar lunar = Lunar.fromDate(DateTime.now());
    String lunarDay = '${lunar.getYearInChinese()}年 ${lunar.getMonthInChinese()}月 ${lunar.getDayInChinese()}';
    return lunarDay;
  }

  @override
  Widget build(BuildContext context) {
    final user = context.watch<UserProvider>().getUserInfo;
    return Scaffold(
      appBar: CommonHeader.appBar(context: context),
      backgroundColor: ColorStyle.pageDefaultColor,
      body: ListView(
        padding: EdgeInsets.fromLTRB(Constant.PAGEPADDING.w, 20.w, Constant.PAGEPADDING.w, 40.w),
        children: [
          Center(child: ClipOval(
            child: Container(
              width: 160.w,
              height: 160.w,
              color: Colors.black,
              child: user?.avatar != null
                ? ExtendedImage.network(user!.avatar!, fit: BoxFit.contain)
                : null
            ),
          )),
          Container(
            padding: EdgeInsets.only(top: 32.w),
            margin: EdgeInsets.symmetric(vertical: 20.w),
            decoration: containerStyle(),
            child: Column(
              children: [
                const CommonShadowTextWidget(txt: '今日生肖运势榜单'),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 40.w),
                  margin: EdgeInsets.symmetric(vertical: 40.w),
                  child: Column(
                    children: [
                      Container(
                        height: 60.w,
                        alignment: Alignment.center,
                        child: Row(
                          children: [
                            const CommonText(txt: '阳历：', weight: FontWeight.bold),
                            CommonText(txt: '$date $weekend')
                          ],
                        ),
                      ),
                      Container(
                        height: 60.w,
                        alignment: Alignment.center,
                        child: Row(
                          children: [
                            const CommonText(txt: '农历：', weight: FontWeight.bold),
                            // CommonText(txt: '二零二四年 十一月 初六')
                            CommonText(txt: transformLunarChineseDay())
                          ],
                        ),
                      ),
                      Container(
                        height: 60.w,
                        alignment: Alignment.center,
                        child: Row(
                          children: [
                            const CommonText(txt: '干支：', weight: FontWeight.bold),
                            CommonText(txt: '${ganZhiResult['ganZhiYear']}年 ${ganZhiResult['ganZhiMonth']}月 ${ganZhiResult['ganZhiDay']}日')
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  height: 256.w,
                  decoration: BoxDecoration(
                    border: Border(top: BorderSide(width: 1.w, color: ColorStyle.borderColor))
                  ),
                  child: Row(
                    children: [
                      Expanded(child: Container(
                        decoration: BoxDecoration(
                            border: Border(right: BorderSide(width: 1.w, color: ColorStyle.borderColor))
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              width: 270.w,
                              height: 80.w,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  color: ColorStyle.themeColor,
                                  border: Border.all(width: 5.w, color: ColorStyle.borderColor),
                                  borderRadius: BorderRadius.all(Radius.circular(60.w))
                              ),
                              child: const CommonText(txt: '今日特吉生肖'),
                            ),
                            SizedBox(height: 24.w),
                            StrokeText(txt: dailyHoroscopeResult?.bestZodiac??'-', fontSize: 60.sp, fontFamily: 'fontZKKLT')
                          ],
                        ),
                      )),
                      Expanded(child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Container(
                            width: 270.w,
                            height: 80.w,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: ColorStyle.themeColor,
                                border: Border.all(width: 5.w, color: ColorStyle.borderColor),
                                borderRadius: BorderRadius.all(Radius.circular(60.w))
                            ),
                            child: const CommonText(txt: '今日次吉生肖'),
                          ),
                          SizedBox(height: 24.w),
                          StrokeText(txt: dailyHoroscopeResult?.goodZodiac??'-', fontSize: 60.sp, fontFamily: 'fontZKKLT')
                        ],
                      )),
                    ],
                  ),
                )
              ],
            ),
          ),
          Container(
            padding: EdgeInsets.symmetric(vertical: 32.w),
            margin: EdgeInsets.only(top: 20.w, bottom: 120.w),
            decoration: containerStyle(),
            child: Column(
              children: [
                CommonShadowTextWidget(txt: '生肖$animalText今日运势'),
                SizedBox(
                  height: 200.w,
                  child: Center(child: StrokeText(txt: animalText, fontSize: 100.sp, fontFamily: 'fontZKKLT')),
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 40.w),
                  margin: EdgeInsets.only(top: 20.w),
                  child: Column(
                    children: [
                      rowItem(txt: '财富运势', num: dailyHoroscopeResult?.wealthHoroscope?.star??0, url: 'assets/icons/icon_luck_wealth', content: dailyHoroscopeResult?.wealthHoroscope?.description ?? '-'),
                      rowItem(txt: '感情运势', num: dailyHoroscopeResult?.loveHoroscope?.star??0, url: 'assets/icons/icon_luck_emotion', content: dailyHoroscopeResult?.loveHoroscope?.description ?? '-'),
                      rowItem(txt: '事业运势', num: dailyHoroscopeResult?.careerHoroscope?.star??0, url: 'assets/icons/icon_luck_cause', content: dailyHoroscopeResult?.careerHoroscope?.description ?? '-'),
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(40.w, 40.w, 40.w, 0),
                  decoration: BoxDecoration(
                    border: Border(top: BorderSide(width: 1.w, color: ColorStyle.borderColor))
                  ),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Column(
                            children: [
                              StrokeText(txt: '幸运位数', fontSize: 30.sp, letterSpacing: 1.5),
                              SizedBox(height: 20.w),
                              CommonText(txt: '${dailyHoroscopeResult?.luckyNumber ?? '-'}'),
                              SizedBox(height: 40.w),
                              StrokeText(txt: '事业贵人', fontSize: 30.sp, letterSpacing: 1.5),
                              SizedBox(height: 20.w),
                              CommonText(txt: dailyHoroscopeResult?.careerBenefactor ?? '-')
                            ],
                          ),
                          Column(
                            children: [
                              StrokeText(txt: '幸运颜色', fontSize: 30.sp, letterSpacing: 1.5),
                              SizedBox(height: 20.w),
                              CommonText(txt: dailyHoroscopeResult?.luckyColor ?? '-'),
                            ],
                          ),
                          Column(
                            children: [
                              StrokeText(txt: '开运方向', fontSize: 30.sp, letterSpacing: 1.5),
                              SizedBox(height: 20.w),
                              CommonText(txt: dailyHoroscopeResult?.fortuneDirection ?? '-')
                            ],
                          )
                        ],
                      ),
                      SizedBox(height: 40.w),
                      CommonText(txt: '以上是根据您今天的生肖运势，所提供的开运颜色、数字饰品、贵人等开运方法，可以让您的自信心加倍，好运能力上升!', fontSize: 30.sp, height: 1.5,)
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
